/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="dnn.xmlhttp.js" assembly="DotNetNuke.WebUtility" />

Type.registerNamespace('$YourCompany$');
    
$YourCompany$.View$safeprojectname$ = function()
{
    //Call Base Method
    $YourCompany$.View$safeprojectname$.initializeBase(this);
    //Member Variables
    this._msgs = {};
    this._divPlayer = null;
    this._mediaUrl = '';
    this._height = '200';
    this._width = '320';
    this._xamlPath = '';
    this._xamlFile = 'media.xaml';
    this._media = null;
    this._host = null;
    this._buttonPanel = null;
    this._textControls = [];

    //create delegates (MAKE SURE YOU CLEAN UP IN dispose!)
    this._onLoadDelegate = Function.createDelegate(this, this._onLoad);
    this._onXAMLLoadDelegate = Function.createDelegate(this, this._onXAMLLoad);
    this._onButtonClickDelegate = Function.createDelegate(this, this._onButtonClick);
    this._onMediaFullScreenChangedDelegate = Function.createDelegate(this, this._onMediaFullScreenChanged);

    //Event Hookup
    Sys.Application.add_load(this._onLoadDelegate);
}

$YourCompany$.View$safeprojectname$.prototype =
{
    //Properties
    get_ns: function() {return this.get_id() + '_';},
    get_msgs: function() {return this._msgs;},
    set_msgs: function(value) {this._msgs = Sys.Serialization.JavaScriptSerializer.deserialize(value);},
    get_mediaUrl: function() {return this._mediaUrl;},
    set_mediaUrl: function(value) {this._mediaUrl = value;},
    get_xamlPath: function() {return this._xamlPath;},
    set_xamlPath: function(value) {this._xamlPath = value;},
    get_xamlFile: function() {return this._xamlFile;},
    set_xamlFile: function(value) {this._xamlFile = value;},
    get_height: function() 
    {
        if (this._host.content.fullScreen)
            return this._host.content.actualHeight;    
        else 
            return this._height;
            
    },
    set_height: function(value) {this._height = value;},
    get_width: function() 
    {
        if (this._host.content.fullScreen)
            return this._host.content.actualWidth;    
        else 
            return this._width;
    },
    set_width: function(value) {this._width = value;},

    //Events
    initialize: function() 
    {
        //Call Base Method    
        $YourCompany$.View$safeprojectname$.callBaseMethod(this, 'initialize');
        
        //create UI
                
        //hookup event handlers
        
    },

    _onLoad: function(src, args)
    {
        //page is completely loaded, you can now access any element
        this._divPlayer = $get(this.get_ns() + 'divPlayer');

        Silverlight.createObject(
                this._xamlPath + this._xamlFile,      // Source property value.
                this._divPlayer,                      // DOM reference to hosting DIV tag.
                this.get_ns() + "media",                   // Unique plug-in ID value.
                {                                     // Plug-in properties.
                    width:this._width,                // Width of rectangular region of plug-in in pixels.
                    height:this._height,              // Height of rectangular region of plug-in in pixels.
                    inplaceInstallPrompt:false,       // Determines whether to display in-place install prompt if invalid version is detected.
                    background:'black',               // Background color of plug-in.
                    isWindowless:'false',             // Determines whether to display plug-in in windowless mode.
                    framerate:'24',                   // MaxFrameRate property value.
                    version:'1.0'                     // Silverlight version.
                },
                {
                    onError:null,                     // OnError property value -- event-handler function name.
                    onLoad:this._onXAMLLoadDelegate  // OnLoad property value -- event-handler function name.
                },
                null,                                 // initParams -- user-settable string for information passing.
                null);                                // Context value -- passed to Silverlight.js onLoad event handlers.
    },

    _onXAMLLoad: function(control, userContext, rootElement)
    {    
        this._host = rootElement.getHost();
        this._media = rootElement.findName('media'); 
        this._media.Stretch = 'Fill';
        this._buttonPanel = rootElement.findName('buttonPanel'); 

        //add xaml event handlers
        this._hookupMediaButton('btnPlay');
        this._hookupMediaButton('btnStop');
        this._hookupMediaButton('btnPause');
        this._hookupMediaButton('btnFullScreen');
		//Note:  there is no addEventListener for onfullScreenChange - odd...
		this._media.getHost().content.onfullScreenChange = this._onMediaFullScreenChangedDelegate;
        
        if (this._mediaUrl.length > 0)
            this._media.source = this._mediaUrl;
        else
            this._media.source = this._xamlPath + '$YourCompany$Logo.wmv';
        this._resize();
		
    },
    
    _onButtonClick: function(sender, args)
    {
        this._updateTextHighlight(sender.Name + 'Text');
        switch (sender.Name)
        {
            case 'btnPlay':
            {
                this._media.play();
                break;
            }
            case 'btnPause':
            {
                this._media.pause();
                break;
            }
            case 'btnStop':
            {
                this._media.stop();
                break;
            }
            case 'btnFullScreen':
            {
                this._host.content.fullScreen = !this._host.content.fullScreen;
                break;
            }
        }
    },
    
    _updateTextHighlight: function(selName)
    {
        for (var i=0; i<this._textControls.length; i++)
        {
            if (this._textControls[i].Name == selName)
                this._textControls[i].Foreground = 'white';
            else
                this._textControls[i].Foreground = 'silver';
        }
    },
    
    _onMediaFullScreenChanged: function(sender, args)
    {
        this._buttonPanel.opacity = (this._host.content.fullScreen ? 0 : 1);
        this._resize();
    },

    //Methods
    getMessage: function(key)
    {
        return this._msgs[key];
    },

    //Private Methods
    _resize: function()
    {
        var panelHeight = new Number(this._buttonPanel.height);
        var PADDING = 10;
        this._media.width = this.get_width();
        this._media.height = new Number(this.get_height()) - panelHeight - PADDING;    
        this._buttonPanel.setValue('Canvas.Top', this._media.height + PADDING);
    },
    
    _hookupMediaButton: function(id, delegate)
    {
        var btn = this._buttonPanel.findName(id);
        btn.addEventListener("MouseLeftButtonDown", this._onButtonClickDelegate);
        var txt = btn.findName(id + 'Text');
        txt.Text = this.getMessage(id); //localize buttons
        this._textControls.push(txt);
    },
    
    dispose: function()
    {
        this._divPlayer = null;
        this._media = null;
        this._host = null;
        this._onLoadDelegate = null;
        this._onXAMLLoadDelegate = null;
        this._onMediaPlayDelegate = null;
        this._onMediaStopDelegate = null;
        this._onMediaPauseDelegate = null;
        this._onMediaFullScreenDelegate = null;
    }
}

//register class and inherit from Sys.Component
$YourCompany$.View$safeprojectname$.registerClass('$YourCompany$.View$safeprojectname$', Sys.Component);
